package com.osgix.common.pojo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <b>权限点(资源)对象</b>
 * <b>@author zqmao 2007-7-11,上午11:22:22</b>
 * <b>version 1.0</b>
 */
public class Resource implements Serializable {

	private static final long serialVersionUID = -5600373761108997723L;
	//retype: ０：表示普通资源组；１：表示根资源组；２：表示下级资源以弹出表格显现;3:表示下级资源在初始化时不装载,而是在双击时将资源添加到资源树
	/**
	 * 普通资源/资源组
	 */
	public static final int GENERAL = 0;
	/**
	 * 根资源组
	 */
	public static final int GROUP_ROOT = 1;
	/**
	 * 子资源以表格形式呈现资源组
	 */
	public static final int GROUP_TABLE = 2;
	/**
	 * 表示下级资源在初始化时不装载,而是在双击时将资源添加到资源树
	 */
	public static final int GROUP_TREE = 3;

	//系统操作和没有动态分组属性的权限点
	public static final int PP_SYS_OPER = 0;

	//拥有属性，允许动态进行分组的权限点
	public static final int PP_DYNAMIC = 1;

	//标识此权限点为一个权限点组
	public static final int PP_GROUP = 2;

	/**
	 * 默认所属应用系统名称
	 */
	public static final String APP_DEFAULT = "OA";

	/**
	 * 资源编号
	 */
	private String resourceId = "";

	/**
	 * 资源名称
	 */
	private String name = "";

	/**
	 * 上级资源ID
	 */
	private String superId;

	/**
	 * URL
	 */
	private String url = "";

	/**
	 * 访问类型
	 */
	private String accessType = "";

	/**
	 * 描述
	 */
	private String note = "";

	/**
	 * 资源呈现类型
	 */
	private int loadType = GENERAL;

	private int ppType = 0;

	/**
	 * 属性对应表 Map<propertyId,Property>
	 */
	private Map<String, Property> propertyMap = new HashMap<String, Property>();

	/**
	 * 所属系统标识
	 */
	private String appName = APP_DEFAULT;

	/**
	 * 
	 *
	 */
	public Resource() {

	}

	/**
	 * 构造函数
	 * @param resource_id
	 */
	public Resource(String resource_id) {
		this.resourceId = resource_id;
	}

	/**
	 * 构造函数
	 * @param id
	 * @param name
	 * @param url
	 * @param superId
	 * @param accessType
	 * @param reType
	 */
	public Resource(String id, String name, String url, String superId, String accessType, String note, int reType,
			int ppType) {
		this.resourceId = id;
		this.name = name;
		this.url = url;
		this.superId = superId;
		this.accessType = accessType;
		this.note = note;
		this.loadType = reType;
		this.ppType = ppType;
	}

	/**
	 * 构造函数
	 * @param id
	 * @param name
	 * @param url
	 * @param superId
	 * @param accessType
	 * @param reType
	 */
	public Resource(String id, String name, String url, String superId, String accessType, String note, int reType,
			int ppType, String appName) {
		this.resourceId = id;
		this.name = name;
		this.url = url;
		this.superId = superId;
		this.accessType = accessType;
		this.note = note;
		this.loadType = reType;
		this.ppType = ppType;
		this.appName = appName;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	public Object clone() {
		Resource newResource = new Resource();
		newResource.setResourceId(resourceId);
		newResource.setName(name);
		newResource.setNote(note);
		newResource.setLoadType(loadType);
		newResource.setSuperId(superId);
		newResource.setUrl(url);
		newResource.setAccessType(accessType);
		newResource.setPpType(ppType);
		newResource.setAppName(appName);

		Iterator<String> keys = propertyMap.keySet().iterator();
		while (keys.hasNext()) {
			String key = keys.next();
			Property property = propertyMap.get(key);
			newResource.putProperty(key, property);
		}

		return newResource;
	}

	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Resource))
			return false;
		Resource res = (Resource) obj;
		if (resourceId == null)
			return false;
		if (!resourceId.equals(res.getResourceId()))
			return false;
		if ((name == null && res.getName() != null) || (name != null && !name.equals(res.getName())))
			return false;
		if ((superId == null && res.getSuperId() != null) || (superId != null && !superId.equals(res.getSuperId())))
			return false;
		if ((url == null && res.getUrl() != null) || (url != null && !url.equals(res.getUrl())))
			return false;
		if ((accessType == null && res.getAccessType() != null)
				|| (accessType != null && !accessType.equals(res.getAccessType())))
			return false;
		if ((note == null && res.getNote() != null) || (note != null && !note.equals(res.getNote())))
			return false;
		if (loadType != res.getLoadType())
			return false;
		if (ppType != res.getPpType())
			return false;
		if (!equalsPropertyMap(res))
			return false;
		if ((appName == null && res.getAppName() != null) || (appName != null && !appName.equals(res.getAppName())))
			return false;
		return true;
	}

	/**
	 * 比较权限点属性是否一致
	 * @param res
	 * @return
	 */
	private boolean equalsPropertyMap(Resource res) {
		Map<String, Property> resMap = res.getPropertyMap();
		if (propertyMap.size() != resMap.size())
			return false;
		Iterator<String> keys = propertyMap.keySet().iterator();
		while (keys.hasNext()) {
			String key = keys.next();
			Property nowProperty = propertyMap.get(key);
			if (!nowProperty.equals(resMap.get(key)))
				return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return name;
	}

	public String getAccessType() {
		return accessType;
	}

	public void setAccessType(String accessType) {
		this.accessType = accessType;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String id) {
		this.resourceId = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public int getLoadType() {
		return loadType;
	}

	public void setLoadType(int reType) {
		this.loadType = reType;
	}

	public String getSuperId() {
		return superId;
	}

	public void setSuperId(String superId) {
		this.superId = superId;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPpType() {
		return ppType;
	}

	public void setPpType(int ppType) {
		this.ppType = ppType;
	}

	/**
	 * 取出一个属性类型对应的属性对象值
	 * @param propertyId
	 * @return
	 */
	public Property getProperty(String propertyId) {
		return propertyMap.get(propertyId);
	}

	/**
	 * 添加一种属性类型的值
	 * @param propertyId
	 * @param property
	 */
	public void putProperty(String propertyId, Property property) {
		propertyMap.put(propertyId, property);
	}

	public Map<String, Property> getPropertyMap() {
		return propertyMap;
	}

	public void setPropertyMap(Map<String, Property> propertyMap) {
		this.propertyMap = propertyMap;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

}
